package com.ws.webpacket.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.ws.webpacket.internet.been.B_Ping;

import java.util.ArrayList;
import java.util.List;

public class SharedPerformanceUtile  {

    private static final String SH_NAME = "SH_NAME";

    public static final String SAVE_STATUS = "save_status";

    public static SharedPreferences getSharedPreference(Context context){
        SharedPreferences sharedPreferences = context.getSharedPreferences(SH_NAME,Context.MODE_PRIVATE);
        return sharedPreferences;
    }

    public static SharedPreferences.Editor getSharedPrederence(Context context){
        SharedPreferences.Editor editor = getSharedPreference(context).edit();
        return editor;
    }

    public static Boolean get(Context context, String key){
        return getSharedPreference(context).getBoolean(key,false);
    }

    public static void put(Context context, String key, Boolean value){
        getSharedPrederence(context).putBoolean(key,value).commit();
    }


    public static void putLoadUrl(Context context, List<B_Ping> loadUrl){
        SharedPreferences.Editor editor = getSharedPrederence(context);
        editor.putInt("loadUrl_count",loadUrl.size());
        for (int i = 0; i < loadUrl.size() ; i++) {
            if(loadUrl.get(i) == null)continue;
            editor.putString("loadUrl_url"+i,loadUrl.get(i).getUrl())
                    .putInt("loadUrl_overTime"+i,loadUrl.get(i).getOverTime())
                    .putInt("loadUrl_index"+i,loadUrl.get(i).getIndex());
        }
        editor.commit();
    }


    public static List<B_Ping> getLoadUrl(Context context){
        SharedPreferences sharedPreferences = getSharedPreference(context);
        int urlCount =sharedPreferences.getInt("loadUrl_count",0);
        List<B_Ping> pings = new ArrayList<>();

        for (int i = 0; i < urlCount; i++) {
            B_Ping ping = new B_Ping();
            ping.setUrl(sharedPreferences.getString("loadUrl_url"+i,null));
            ping.setOverTime(sharedPreferences.getInt("loadUrl_overTime"+i,0));
            ping.setIndex(sharedPreferences.getInt("loadUrl_index"+i,0));
            pings.add(ping);
        }

        return pings;
    }



}
